#!/bin/bash

# 创建项目目录结构
mkdir -p app/{api/v1/endpoints,core,models} tests manifests

# 创建所有必要的Python包初始化文件
touch app/__init__.py
touch app/api/__init__.py
touch app/api/v1/__init__.py
touch app/api/v1/endpoints/__init__.py
touch app/core/__init__.py
touch app/models/__init__.py
touch tests/__init__.py

# 创建核心配置文件
cat > app/core/config.py << EOF
from pydantic import BaseSettings

class Settings(BaseSettings):
    app_name: str = "FastAPI K8S Manager"
    app_version: str = "v0.1.0"
    
    class Config:
        env_file = ".env"

settings = Settings()
EOF

# 创建Kubernetes客户端配置
cat > app/core/k8s.py << EOF
from kubernetes import client, config

try:
    config.load_incluster_config()
except config.ConfigException:
    try:
        config.load_kube_config()
        print("Loaded local kubeconfig for development.")
    except config.ConfigException:
        raise RuntimeError("Could not configure kubernetes python client")

v1_core = client.CoreV1Api()
apps_v1 = client.AppsV1Api()
EOF

# 创建主应用文件
cat > app/main.py << EOF
from fastapi import FastAPI
from app.core import config as app_config
from app.api.v1.api import api_router

app = FastAPI(
    title=app_config.settings.app_name,
    version=app_config.settings.app_version
)

app.include_router(api_router, prefix="/api/v1")

@app.get("/")
@app.get("/health")
async def root():
    return {"status": "healthy", "message": "FastAPI K8S Manager is running."}
EOF

# 创建API路由文件
cat > app/api/v1/api.py << EOF
from fastapi import APIRouter

api_router = APIRouter()

# 后续在这里添加路由
# from app.api.v1.endpoints import pods
# api_router.include_router(pods.router, prefix="/pods", tags=["pods"])
EOF

# 创建需求文件
cat > requirements.txt << EOF
fastapi==0.83.0
uvicorn[standard]==0.15.0
kubernetes==26.1.0
python-dotenv==0.19.0
EOF

# 创建开发启动脚本
cat > run.py << EOF
import uvicorn

if __name__ == "__main__":
    uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)
EOF

# 创建环境变量示例文件
cat > .env.example << EOF
APP_NAME=FastAPI K8S Manager
APP_VERSION=v0.1.0
EOF

# 创建Dockerfile
cat > Dockerfile << EOF
FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "run.py"]
EOF

# 创建.dockerignore文件
cat > .dockerignore << EOF
__pycache__
*.pyc
*.pyo
*.pyd
.Python
env
venv
.venv
.env
.git
.mypy_cache
.pytest_cache
.history
EOF

# 创建README文件
cat > README.md << EOF
# FastAPI K8S Manager

一个使用 FastAPI 0.83.0 和 Kubernetes Python客户端构建的K8S管理界面。

## 开发

1. 创建虚拟环境: \`python -m venv venv\`
2. 激活虚拟环境: \`source venv/bin/activate\` (Linux/Mac) 或 \`venv\\Scripts\\activate\` (Windows)
3. 安装依赖: \`pip install -r requirements.txt\`
4. 复制环境变量: \`cp .env.example .env\`
5. 运行开发服务器: \`python run.py\`

## API文档

访问 \`http://localhost:8000/docs\` 查看Swagger UI文档。
EOF

echo "项目结构创建完成！"
echo "请执行以下命令初始化项目:"
echo "1. python -m venv venv"
echo "2. source venv/bin/activate (Linux/Mac) 或 venv\\Scripts\\activate (Windows)"
echo "3. pip install -r requirements.txt"
echo "4. cp .env.example .env"
echo "5. python run.py"
